package com.orange.wwwapi.controller.coupon;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.orange.core.pojo.PageReqDto;
import com.orange.core.pojo.R;
import com.orange.core.pojo.entity.coupon.Coupon;
import com.orange.core.pojo.req.coupon.CouponPageReq;
import com.orange.core.pojo.rsp.usercoupon.CouponOrderPayRsp;
import com.orange.core.pojo.rsp.usercoupon.CouponOrderRsp;
import com.orange.core.service.coupon.CouponService;
import com.orange.core.service.user.UserCouponService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * 优惠券
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/coupon")
public class CouponController {

    private final CouponService couponService;
    private final UserCouponService userCouponService;

    /**
     * 查询
     */
    @PostMapping("/page")
    public R<Page<Coupon>> page(@RequestBody PageReqDto<CouponPageReq> req) {
        if (req.getCondition() == null) {
            req.setCondition(new CouponPageReq());
        }
        req.getCondition().setShelf(1);
        return R.ok(couponService.page(req));
    }

    /**
     * 创建订单
     *
     * @param couponId 优惠券id
     */
    @PostMapping("/createOrder/{couponId}")
    public R<CouponOrderRsp> generateOrder(@PathVariable int couponId) {
        return R.ok(userCouponService.generateOrder(couponId));
    }

    /**
     * 支付
     *
     * @param orderNo 订单号
     */
    @PostMapping("/pay/{orderNo}")
    public R<CouponOrderPayRsp> pay(@PathVariable String orderNo) {
        return R.ok(userCouponService.pay(orderNo));
    }
}
